<!---
	TEMPLATE: userForm_process.cfm
	AUTHOR: Matt Woodward (mpwoodward@gmail.com)
	DATE: 2/20/2005
	PURPOSE: processing page for user form; interacts with Hibernate in CFML
	CHANGE HISTORY:
		* 2/20/2005: template created (Matt Woodward)
		* 2/21/2005: template updated to handle "update" actions
						(Matt Woodward)
--->

<!--- set defaults for form data --->
<cfparam name="form.firstName" type="string" default="" />
<cfparam name="form.lastName" type="string" default="" />
<cfparam name="form.email" type="string" default="" />
<cfparam name="form.action" type="string" default="" />

<!--- make sure we have the required form data --->
<cfif form.firstName EQ "" OR form.lastName EQ "" OR form.email EQ "" 
	OR form.action EQ "">
	<script type="text/javascript">
		alert("We're sorry, required data was not passed to this page.\n
				Please try again");
		history.go(-1);
	</script>
	
	<cfabort />
</cfif>

<cfscript>
	// get Hibernate session factory
	sessionFactory = Application.hibernateConfig.buildSessionFactory();
	
	// open a session and start the transaction
	mySession = sessionFactory.openSession();
	tx = mySession.beginTransaction();
	
	// take appropriate action based on form's action value
	switch(form.action) {
		case "Add":
			// instantiate a User object with the form data and persist it
			user = CreateObject("java", "User").init(form.firstName, 
													form.lastName, 
													form.email, form.middlename);
			mySession.save(user);
			break;
		
		case "Update":
			/* need to figure out how better to handle this so we don't have
				to get the object first, then run the update */
			// get the user so we have the ID since it's a private setter
			hqlQueryString = "from User user where userID = " & form.id;
			user = mySession.createQuery(hqlQueryString).uniqueResult();
			
			// set user data
			user.setFirstName(form.firstName);
			user.setLastName(form.lastName);
			user.setMiddleName(form.middleName);
			user.setEmail(form.email);
			
			// update the user
			mySession.update(user);
			break;
		
		case "Delete":
			/* again, at this point I have to get the user again--figure
				out a better way to do this */
			hqlQueryString = "from User user where userID = " & form.id;
			user = mySession.createQuery(hqlQueryString).uniqueResult();
			
			// delete the user
			mySession.delete(user);
			break;
	}
	
	// commit the transaction
	tx.commit();
	
	// clean up
	mySession.close();
	sessionFactory.close();
</cfscript>

<html>
<head>
	<title>Process User Form</title>
</head>
<body>
	<h3>Process User Form</h3>
	
	<cfswitch expression="#form.action#">
		<cfcase value="Add">
			<p>Hibernate successfully persisted the new User object.</p>
		</cfcase>
		
		<cfcase value="Update">
			<p>Hibernate successfully persisted the updated User object.</p>
		</cfcase>
		
		<cfcase value="Delete">
			<p>Hibernate successfully deleted the User from the database.</p>
		</cfcase>
	</cfswitch>
	
	<ul>
		<li><a href="index.cfm">Main Menu</a></li>
		<li><a href="userForm.cfm">Add User</a></li>
	</ul>
</body>
</html>
